<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;

use think\Model;
use think\Db;
use think\Cache;

class Department extends Model
{
    /**
     * 缓存部门信息
     */
    protected function departmentcache ()
    {
        $result = Db::name('sys_department')->select();
        // 以id为键
        foreach ($result as $val) {
            $department[$val['department_id']] = $val;
        }
        Cache::set('department', $department);
    }


    /**
     * 增加部门
     * @param  [array] $data [数据参数]
     * @return [bool]        [添加成功/失败]
     */
    public function insertDepartment($data)
    {
        $result = Db::name('sys_department')->insert($data);
        $this->departmentcache();
        if ($result){
            return true;
        } else {
            return false;
        }
    }


    /**
     * 删除部门
     * @param  [str] $id [被删除的主键]
     * @return [bool]    [删除成功/失败]
     */
    public function deleteDepartment($id)
    {
        $result = Db::name('sys_department')->where("department_id = $id")->delete();
        $this->departmentcache();
        if ($result){
            return true;
        } else {
            return false;
        }
    }


    /**
     * 查询部门信息
     * @param  [str] $where [查询条件]
     * @return [array]      [查询结果]
     */
    public function departmentData ($where = '') 
    {
        if ($where) {
            $result = Db::name('sys_department')->where($where)->select();
        } else {
            $result = Db::name('sys_department')->select();
        }
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }


    /**
     * 编辑数据
     * @param  [array] $data [需要修改的信息]
     * @return [bool]        [是否修改成功]
     */
    public function updataDepartment ($data) 
    {
        $result = Db::name('sys_department')->update($data);
        $this->departmentcache();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 部门递归 select数据
     * @param  string $department_pid [部门pid]
     * @param  string $id             [部门id 可选]
     * @return [str/bool]             [返回部门option字符串/失败返回false]
     */
    public function recursionDepartment($department_pid = "0") 
    {
        $where = "department_pid = $department_pid";
        $result = $this->departmentData($where);
        if ($result) {
            foreach ($result as &$val) {
                $val['children'] = $this->recursionDepartment($val['department_id']);
            }
            return $result;
        } else {
            return false;
        }
    }


    /**
     * 处理部门数组
     * @param  [array] $array [需要处理的数组]
     * @param  string $id     [部门id]
     * @param  string $mark   [层级标示符]
     * @return [string]       [拼接好的option字符串]
     */
    public function recursionDepartmentArr ($array, $id="", $mark="")
    {
        $str = '';
        $mark .= "|--";
        foreach ($array as $val) {
            if ($val['department_id'] == $id) {
                $str .= '<option value="'.$val['department_id'].'" selected>'.$mark.$val['department_name'].'</option>';
            } else {
                $str .= '<option value="'.$val['department_id'].'">'.$mark.$val['department_name'].'</option>';
            }
            if ($val['children'] != null) {
                $str .= $this->recursionDepartmentArr($val['children'], $id, $mark);
            }
        }
        return $str;
    }


}